using System;
using Newtonsoft.Json;
using Karma.Framework.Core.Domain.Tools.Import.Validation;

namespace Karma.Framework.Core.Domain.Tools.Import.Handler
{
    public class ErrorDescriptionConverter : JsonConverter
    {
        public override void WriteJson (JsonWriter writer, object value)
        {
            var error = (ErrorDescription) value;

            writer.WriteStartObject ();
            writer.WritePropertyName("Line");
            writer.WriteValue(error.Line);
            writer.WritePropertyName("Column");
            writer.WriteValue(error.Column);
            writer.WritePropertyName("Property");
            writer.WriteValue(error.Property.Name);
            writer.WritePropertyName("Description");
            writer.WriteValue(error.Description);
            writer.WritePropertyName("Type");
            switch (error.Type)
            {
                case ErrorType.Format:
                    writer.WriteValue("Formato");
                    break;
                case ErrorType.Data:
                    writer.WriteValue("Datos");
                    break;
            }
            writer.WritePropertyName("Severity");
            switch (error.Severity)
            {
                case ErrorSeverity.Warning:
                    writer.WriteValue("Alerta");
                    break;
                case ErrorSeverity.Error:
                    writer.WriteValue("Error");
                    break;
                case ErrorSeverity.Fatal:
                    writer.WriteValue("Fatal");
                    break;
            }
            writer.WriteEndObject();
        }

        public override object ReadJson (JsonReader reader, Type objectType)
        {
            throw new InvalidOperationException();
        }

        public override bool CanConvert (Type objectType)
        {
            return typeof (ErrorDescription).IsAssignableFrom (objectType);
        }
    }
}